< previous page page_226 next page >

Page 226
Given a pointer, you can use the lstrlen function to obtain the length of the string (not including the NULL character). You can then preinitialize the string to the necessary length. This function reverses the operation shown in the CreateAnsiArray function by using the lstrcpy function and passing a Visual Basic string as the first variable and a pointer as the second variable. This requires a different declaration for lstrcpy, so we create a new alias for the function that is declared as follows:
Private Declare Function lstrcpystring Lib "kernel32" Alias "lstrcpyA" _
(ByVal lpString1 As String, ByVal lpString2 As Long) As Long
Finally, the NULL termination character is stripped from the string.
The agGetStringFromPointer function in the apigid32.dll library (included in Appendix C of this book) performs the same operation and is far more efficient, since it works with a single API call.
This leaves us with the following version of the GetResourceInfo function, which works:
' Get the network provider information
Private Sub GetResourceInfo(resourcename As String)
   Dim nr As NETRESOURCE
   Dim ResourceNameArray() As Byte
   Dim lpSystem As Long
   Dim OutputBufferSize As Long
   Dim OutputBuffer() As Byte
   Dim res As Long

   lstResource.Clear
   If resourcename = "" Then Exit Sub

   nr.dwType = RESOURCETYPE_DISK
   ' Create a buffer with the ANSI local name
   Call CreateAnsiArray(resourcename, ResourceNameArray)
   nr.lpRemoteName = VarPtr(ResourceNameArray(0))

   ' Prep the buffer
   OutputBufferSize = 1024
   Do
      ReDim OutputBuffer(OutputBufferSize)
      res = WNetGetResourceInformation(nr, OutputBuffer(0), _
            OutputBufferSize, lpSystem)
    Loop While res = ERROR_MORE_DATA
    If res <> 0 Then

 
< previous page page_226 next page >